package master;

import user.DBUtil;
import user.UserDao;
import employee.Employee;
import user.User;
import master.EmployeeService;
import user.ToolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.SQLException;

public class RegFrm extends JFrame {
    private JTextField nameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JLabel nameMsg;
    private JLabel passwordMsg;
    private JLabel confirmPasswordMsg;
    private EmployeeService employeeService = new EmployeeService();
    private JRadioButton rdbtnNewRadioButton;
    DBUtil c3p0Util = new DBUtil(); // 连接数据库对象，获得connection
    UserDao userDao = new UserDao(); // Java与数据库交互

    public RegFrm() {
        initUI();
    }

    private void initUI() {
        setTitle("企业员工管理系统-用户注册");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(400, 350);
        setLocationRelativeTo(null);
        getContentPane().setLayout(null);

        // 标题
        JLabel titleLabel = new JLabel("注册", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑",Font.BOLD, 18));
        titleLabel.setBounds(0, 20, 400, 30);
        add(titleLabel);

        // 姓名标签和文本框
        JLabel nameLabel = new JLabel("用户名");
        nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        nameLabel.setBounds(50, 70, 80, 25);
        add(nameLabel);

        nameField = new JTextField();
        nameField.setBounds(140, 70, 200, 25);
        add(nameField);

        nameMsg = new JLabel();
        nameMsg.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        nameMsg.setBounds(140, 95, 200, 20);
        add(nameMsg);

        // 用户名验证监听器
        nameField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                String ename = nameField.getText().trim();
                if (ToolUtil.isEmpty(ename)) {
                    nameMsg.setText("用户名不能为空");
                    nameMsg.setForeground(Color.RED);
                }
                else if (employeeService.isNameExist(ename)) {
                    nameMsg.setText("用户名已存在");
                    nameMsg.setForeground(Color.RED);
                }
                else {
                    nameMsg.setText("√ 用户名可用");
                    nameMsg.setForeground(Color.GREEN);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                nameMsg.setText("");
            }
        });

        // 密码标签和文本框
        JLabel passwordLabel = new JLabel("密码");
        passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        passwordLabel.setBounds(50, 130, 80, 25);
        add(passwordLabel);

        passwordField = new JPasswordField();
        passwordField.setBounds(140, 130, 200, 25);
        add(passwordField);

        passwordMsg = new JLabel();
        passwordMsg.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        passwordMsg.setBounds(140, 155, 200, 20);
        add(passwordMsg);

        // 密码验证监听器
        passwordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                String pwd = new String(passwordField.getPassword());
                if (ToolUtil.isEmpty(pwd)) {
                    passwordMsg.setText("密码不能为空");
                    passwordMsg.setForeground(Color.RED);
                } else if (!ToolUtil.isPasswordValid(pwd)) {
                    passwordMsg.setText("数字");
                    passwordMsg.setForeground(Color.RED);
                } else {
                    passwordMsg.setText("格式正确");
                    passwordMsg.setForeground(Color.GREEN);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                passwordMsg.setText("");
            }
        });

        // 确认密码标签和文本框
        JLabel confirmPasswordLabel = new JLabel("确认密码");
        confirmPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        confirmPasswordLabel.setBounds(50, 190, 80, 25);
        add(confirmPasswordLabel);

        confirmPasswordField = new JPasswordField();
        confirmPasswordField.setBounds(140, 190, 200, 25);
        add(confirmPasswordField);

        confirmPasswordMsg = new JLabel();
        confirmPasswordMsg.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        confirmPasswordMsg.setBounds(140, 215, 200, 20);
        add(confirmPasswordMsg);

        // 确认密码验证监听器
        confirmPasswordField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                String pwd = new String(passwordField.getPassword());
                String confirmPwd = new String(confirmPasswordField.getPassword());

                if (ToolUtil.isEmpty(confirmPwd)) {
                    confirmPasswordMsg.setText("请确认密码");
                    confirmPasswordMsg.setForeground(Color.RED);
                } else if (!confirmPwd.equals(pwd)) {
                    confirmPasswordMsg.setText("两次密码不一致");
                    confirmPasswordMsg.setForeground(Color.RED);
                } else {
                    confirmPasswordMsg.setText("密码匹配");
                    confirmPasswordMsg.setForeground(Color.GREEN);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                confirmPasswordMsg.setText("");
            }
        });

        // 注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        registerButton.setBounds(150, 250, 100, 30);
        registerButton.addActionListener(new RegisterAction());
        add(registerButton);
    }

    private class RegisterAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String name = nameField.getText().trim();
            String password = new String(passwordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());

            // 验证输入
            if (ToolUtil.isEmpty(name)) {
                JOptionPane.showMessageDialog(RegFrm.this, "请输入用户名");
                return;
            }

            if (ToolUtil.isEmpty(password)) {
                JOptionPane.showMessageDialog(RegFrm.this, "请输入密码");
                return;
            }

//            if (!ToolUtil.isPasswordValid(password)) {
//                JOptionPane.showMessageDialog(RegFrm.this, "密码格式");
//                return;
//            }

            if (!password.equals(confirmPassword)) {
                JOptionPane.showMessageDialog(RegFrm.this, "两次输入的密码不一致");
                return;
            }

            // 创建员工对象
            Employee employee = new Employee();
            employee.setEname(name);
//            employee.setPassword(password);

            // 调用服务层注册
//            if (employeeService.register(employee)) {
//                JOptionPane.showMessageDialog(RegFrm.this, "注册成功");
//                dispose(); // 关闭注册窗口
////                new LoginFrm().setVisible(true); // 打开登录窗口
//            } else {
//                JOptionPane.showMessageDialog(RegFrm.this, "注册失败，用户名可能已存在");
//            }
            try {
                boolean isSuccess = employeeService.register(employee);
                if (isSuccess) {
                    JOptionPane.showMessageDialog(RegFrm.this, "注册成功");
                    dispose();
//                new LoginFrm().setVisible(true);
                } else {
                    if (employeeService.isNameExist(name)) {
                        JOptionPane.showMessageDialog(RegFrm.this, "用户名已存在");
                    } else {
                        JOptionPane.showMessageDialog(RegFrm.this, "注册失败，请重试");
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(RegFrm.this, "系统错误：" + ex.getMessage());
            }
        }
    }

//    protected void RegCheck(ActionEvent e) {
//        // 1.获取填写的信息
//        String username = nameField.getText();
//        String password = passwordField.getText();
//        if (ToolUtil.isEmpty(username) || ToolUtil.isEmpty(password)) {
//            JOptionPane.showMessageDialog(null,"请输入相关信息");
//            return;
//        }
//
//        // 2.用输入信息创建user对象
//        User user = new User();
//        user.setUsername(username);
//        user.setPassword(password);
//
//        // 3.连接数据库，添加注册用户
//        Connection conn = null;
//        try {
//            conn = c3p0Util.getConnection();
//            int i = UserDao.addUser(conn, user); // 连接数据库，添加注册用户
//            if (i == 2) {
//                JOptionPane.showMessageDialog(null, "该用户名已存在，请重新注册");
//            } else if (i == 0) {
//                JOptionPane.showMessageDialog(null, "注册失败");
//            } else {
//                JOptionPane.showMessageDialog(null, "注册成功");
////                frame.close();
////                new LoginFrm();
//            }
//        } catch (SQLException ex) {
//            ex.printStackTrace();
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        } finally {
//            try {
//                c3p0Util.closeConnection(conn);
//            } catch (Exception ex) {
//                ex.printStackTrace();
//            }
//        }
//    }

    public static void main(String[] args) {
        EventQueue.invokeLater(()->{
            RegFrm frame = new RegFrm();
            frame.setVisible(true);
        });
    }
}
